package swings.panels;

import entity.CommonParam;
import swings.labels.AddParamLabel;
import swings.labels.HeaderLabel;
import swings.labels.LimpidLabel;
import swings.labels.ParamLabel;
import swings.others.CommonScrollPane;
import utils.ImageUtils;

import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class HeadersPanel extends CommonPanel {

    private CommonPanel main = new CommonPanel();
    private LimpidLabel bgLabel = new LimpidLabel(ImageUtils.common,0, 0, WorkPanel.width-10, WorkPanel.height/2-80);
    private AddParamLabel addParamLabel = new AddParamLabel(105, this);
    private CommonScrollPane jScrollPane = new CommonScrollPane(0,35, WorkPanel.width-10, WorkPanel.height/2-150);
    private int y = 35;
    private int symbol = 0;
    private Map<Integer, ParamLabel> paramMap = new HashMap<>();

    public HeadersPanel() throws IOException {
        jScrollPane.getViewport().add(main);
        HeaderLabel headerLabel = new HeaderLabel(5, "KEY", "VALUE", "DESCRIPTION");

        ParamLabel cache = new ParamLabel(5, symbol, "Content-Type", "application/json;chert=UTF-8", "", this);
        paramMap.put(symbol, cache);
        symbol++;
//        cache.setUnEditable();
        main.add(cache);
        addParamLabel();
//        for (int i=0;i<5;i++){
//            ParamLabel cache = new ParamLabel(5 + i*30, "", "", "");
//            main.add(cache);
//        }

        main.setBorder(null);
        main.setBounds(0,35, WorkPanel.width-10, WorkPanel.height/2-115);
        main.setPreferredSize(new Dimension(WorkPanel.width-10,31*35+15));
        this.add(headerLabel);
        this.add(addParamLabel);
        this.add(jScrollPane);
        this.setBounds(5,81, WorkPanel.width-10, WorkPanel.height/2-80);
        this.add(bgLabel);
        this.setBorder(null);
    }

    public HeadersPanel(Map<String, String> data) throws IOException {
        CommonScrollPane jScrollPane = new CommonScrollPane(0,35, WorkPanel.width-10, WorkPanel.height/2-85);
        jScrollPane.getViewport().add(main);
        HeaderLabel headerLabel = new HeaderLabel(5, "KEY", "VALUE");

        main.add(new ParamLabel(WorkPanel.height/2 + 66, y, "", "", "", this));
        main.setBorder(null);
        main.setBounds(0, WorkPanel.height/2 + 20, WorkPanel.width-10, WorkPanel.height/2-15);
        main.setPreferredSize(new Dimension(WorkPanel.width-10,31*35+15));
        this.add(headerLabel);
//        this.add(addParamLabel);
        this.add(jScrollPane);
        this.setBounds(5, WorkPanel.height/2 + 46, WorkPanel.width-10, WorkPanel.height/2+20);
        this.bgLabel.setBounds(0, 0, WorkPanel.width-10, WorkPanel.height/2-50);
        this.add(bgLabel);
        this.setBorder(null);
    }

    public void newParamLabel() throws IOException {
        this.remove(bgLabel);
        addParamLabel();
        this.add(bgLabel);
        this.invalidate();
        this.validate();
        this.repaint();
    }

    public void addParamLabel() throws IOException {
        ParamLabel cache = new ParamLabel(y, symbol, "", "", "", this);
        paramMap.put(symbol, cache);
        symbol++;
        main.add(cache);
        y += 30;
    }

    @Override
    public void removeParamLabel(int symbol){
        y = 5;
        main.removeAll();
        paramMap.remove(symbol);
        Set<Map.Entry<Integer, ParamLabel>> entries = paramMap.entrySet();
        for (Map.Entry<Integer, ParamLabel> entry : entries) {
            ParamLabel cache = entry.getValue();
            cache.setY(y);
            main.add(cache);
            y += 30;
        }
        this.remove(addParamLabel);
        this.remove(jScrollPane);
        this.remove(bgLabel);
        addParamLabel = new AddParamLabel(y + 40, this);
        this.add(addParamLabel);
        this.add(jScrollPane);
        this.add(bgLabel);
        refresh();
    }

    public ArrayList<CommonParam> getData(){
        ArrayList<CommonParam> result = new ArrayList<>();
        Set<Map.Entry<Integer, ParamLabel>> entries = paramMap.entrySet();
        for (Map.Entry<Integer, ParamLabel> entry : entries) {
            ParamLabel cache = entry.getValue();
            if (cache.isChosen){
                CommonParam data = new CommonParam();
                data.setKey(cache.keyField.getText());
                data.setValue(cache.valueField.getText());
                data.setDescription(cache.descriptionField.getText());
                result.add(data);
            }
        }
        return result;
    }

    public void setData(ArrayList<CommonParam> data) throws IOException {
        if (null != data){
            main.removeAll();
            paramMap.clear();
            symbol = 0;
            y = 5;
            for (int i=0;i<data.size();i++){
                ParamLabel cache = new ParamLabel(5 + i*30, i, data.get(i).getKey(), data.get(i).getValue(),data.get(i).getDescription(),this);
                paramMap.put(symbol, cache);
                symbol++;
                main.add(cache);
                y += 30;
            }
            this.remove(addParamLabel);
            this.remove(jScrollPane);
            this.remove(bgLabel);
            addParamLabel = new AddParamLabel(y + 40, this);
            this.add(addParamLabel);
            this.add(jScrollPane);
            this.add(bgLabel);
            refresh();
        }
    }

    public void setData(Map<String, String> data) throws IOException {
        int i=0;
        if (null != data){
            main.removeAll();
            paramMap.clear();
            Set<Map.Entry<String, String>> entries = data.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                String key = entry.getKey();
                String value = entry.getValue();
                if (null!=key && !key.equals("")){
                    ParamLabel cache = new ParamLabel(5 + i*30, i, key, value);
                    paramMap.put(symbol, cache);
                    symbol++;
                    main.add(cache);
                    i++;
                }
            }
            this.remove(addParamLabel);
            this.remove(jScrollPane);
            this.remove(bgLabel);
            addParamLabel = new AddParamLabel(y + 40, this);
            this.add(addParamLabel);
            this.add(jScrollPane);
            this.add(bgLabel);
            refresh();
        }
    }

    public void removeAddParamLabel(){
        this.remove(addParamLabel);
    }

    public void refresh(){
        this.invalidate();
        this.validate();
        this.repaint();
    }

}
